package com.fejal.managed.Conversor;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.caelum.stella.format.CPFFormatter;
import br.com.caelum.stella.format.Formatter;


@FacesConverter(value="converteCPF", forClass=String.class) 
public class ConverteCPF implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		return arg2;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		
		String cpf = (String) arg2;
		Formatter formatter = new CPFFormatter(); 
		
		if (cpf != null){
			cpf = formatter.format(cpf);	

		}		
		
		return cpf;
	}

}
